package com.futures.questionbank.main;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.futures.questionbank.R;
import com.futures.questionbank.adapter.BaseViewHolder;
import com.futures.questionbank.adapter.ChapterAdpter;
import com.futures.questionbank.db.SQLdm;
import com.futures.questionbank.db.table.chapter;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv;
    private List<chapter> users;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //打开数据库输出流
        SQLdm s = new SQLdm();
        SQLiteDatabase db =s.openDatabase(getApplicationContext());
        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<chapter> users = SQLite.select().
                        from(chapter.class).
                        queryList();
                Log.i("Main",users.size() + "");
                if (users.size() > 0){
                    for (chapter user : users) {
                        Log.i("Main",user.getTitle());
                    }
                }
            }
        });
    }


    private void initView() {
        users = SQLite.select().
                from(chapter.class).
                queryList();
        rv =  findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        rv.setItemAnimator(new DefaultItemAnimator());
        ChapterAdpter chapterAdpter = new ChapterAdpter(this, users, new TypeItemListener());
        rv.setAdapter(chapterAdpter);
        /*FlowManager.getDatabase(AppDataBase.class).executeTransaction(new ITransaction() {
            @Override
            public void execute(DatabaseWrapper databaseWrapper) {
                for (int i=0;i<100;i++){
                    UserModel userModel=new UserModel();
                    userModel.setName("UserModel");
                    userModel.setAge(new Random().nextInt(100));
                    userModel.save(databaseWrapper);
                }
            }
        });*/
    }

    class TypeItemListener implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
            Intent intent = new Intent();
            intent.putExtra("index", users.get(position).getIndex());
            intent.setClass(MainActivity.this,SectionActivity.class);
            startActivity(intent);
            }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }
}
